﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using System.Data.Linq;

namespace SurveySystem.Core
{
    public static class UsersBLL
    {

        private static SurveySystemDataContext DCUSR(string userName, out User user)
        {
            SurveySystemDataContext dc = CommonUtils.GetDc();
            user = dc.Users.FirstOrDefault(u => u.name == userName);
            if (user == null)
                throw new Exception("No such user!");
            return dc;
        }

        public static void RegisterUser(MembershipUser membershipUser, string fullName, string mobilePhoneNumber,
            bool getEmailOnNewSurvey, bool getSmsOnNewSurvey, bool getEmailOnExpiration, bool getSmsOnExpiration)
        {
            using (SurveySystemDataContext dc = CommonUtils.GetDc())
            {
                User user = new User()
                {
                    id = (Guid)membershipUser.ProviderUserKey,
                    name = membershipUser.UserName,
                    email = membershipUser.Email,

                    mobile = mobilePhoneNumber,
                    fullName = fullName,
                    getEmailOnExpiration = getEmailOnExpiration,
                    getSmsOnExpiration = getSmsOnExpiration,
                    getEmailOnNewSurvey = getEmailOnNewSurvey,
                    getSmsOnNewSurvey = getSmsOnNewSurvey
                };
                dc.Users.InsertOnSubmit(user);
                dc.SubmitChanges();
            }
        }

        public static void UnregisterUser(string userName)
        {
            User user;
            using (SurveySystemDataContext dc = DCUSR(userName, out user))
            {
                dc.Members.DeleteAllOnSubmit(user.Members);
                dc.Users.DeleteOnSubmit(user);
                dc.SubmitChanges();
            }
        }

        public static User GetUser(string userName)
        {
            User user;
            using (DCUSR(userName, out user))
            {
                return user;
            }
        }

        public static void UpdateUser(string userName, User userData)
        {
            User original;
            using (SurveySystemDataContext dc = DCUSR(userName, out original))
            {
                original.email = userData.email;
                original.fullName = userData.fullName;
                original.getEmailOnExpiration = userData.getEmailOnExpiration;
                original.getEmailOnNewSurvey = userData.getEmailOnNewSurvey;
                original.getSmsOnExpiration = userData.getSmsOnExpiration;
                original.getSmsOnNewSurvey = userData.getSmsOnNewSurvey;
                original.mobile = userData.mobile;
                CommonUtils.SubmitAndHandleConflicts(dc);
            }
        }

        public static User GetUserByEmail(string email)
        {
            SurveySystemDataContext dc = CommonUtils.GetDc();
            return dc.Users.FirstOrDefault(u => u.email == email);
        }

        public static User GetUserByMobile(string mobile)
        {
            SurveySystemDataContext dc = CommonUtils.GetDc();
            return dc.Users.FirstOrDefault(u => u.mobile == mobile);
        }
        //public static void ChangeUser(string userName, string email,

        public static IEnumerable<User> GetUsers()
        {
            SurveySystemDataContext dc = CommonUtils.GetDc();

            return from u in dc.Users
                   orderby u.name
                   select u;

        }

        public static IEnumerable<User> GetUsersPaged(int maximumRows, int startRowIndex)
        {
            return GetUsers().Skip(startRowIndex).Take(maximumRows);
        }

        public static int GetUsersCount()
        {
            return GetUsers().Count();
        }

        public static User GetUserById(string p)
        {
            SurveySystemDataContext dc = CommonUtils.GetDc();
            Guid g = new Guid(p);
            return dc.Users.FirstOrDefault(u => u.id == g);
        }
    }
}
